<?php
class TreeNode {
    public $val = null;
    public $left = null;
    public $right = null;
    function __construct($value) {
        $this->val = $value;
    }
}

class Solution {
    /**
     * @param Node $node 遍历的节点
     * @param string $parent_path 父节点组成的路径
     * @param array $path_list 父节点列表
     * @return array
     */
    function construct_paths($node, $parent_path, $path_list) {
        $symbol = '->';
        $current_path = $parent_path == '' ? $node->val : $parent_path.$symbol.$node->val;
        if ($node->left == null && $node->right == null) {
            // 这里是因为返回值强制为字符串，如果root的val不是字符串就必须转换一下，否则通不过测试
           $path_list[] = is_int($current_path) ? strval($current_path) : $current_path;
        }
        if ($node->left) {
            $path_list = $this->construct_paths($node->left, $current_path, $path_list);
        }
        if ($node->right) {
            $path_list = $this->construct_paths($node->right, $current_path, $path_list);
        }
        return $path_list;
    }

    function binaryTreePaths($root) {
        return $this->construct_paths($root, '', array());
    }
}